package com.yc.url;

import java.io.*;
import java.net.Socket;

/**
 * @author zwei
 * @create 2024-07-20 14:22
 */
public class Test1_telnet_baidu {
    public static void main(String[] args) {
        String website = "www.baidu.com";
        int port = 80;
        String http = "GET / HTTP/1.0\r\n\r\n";   //应用层协议
        try (
                Socket socket = new Socket(website, port);
                OutputStream oos = socket.getOutputStream();
                InputStream iis = socket.getInputStream();
        ) {
            oos.write(http.getBytes());
            oos.flush();
            ByteArrayOutputStream baos=new ByteArrayOutputStream();
            byte[]bs=new byte[10*1024];
            int length=-1;
            while (   (length=iis.read(bs,0,bs.length))!=-1  ){
                //先把此次读取到的数据通过baos存到内存中
                baos.write(bs,0,length);
            }
            baos.flush();
            byte[] bytes = baos.toByteArray();
            String str=new String(bytes);
            System.out.println(str);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
